////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////Dataset with autonomy as labor characteristic by occupation////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
use "[FOLDER]\Firm_Labor_Occ_Intermediate.dta", replace

replace EffUnits=EffUnits*tjomf_y_av

///////////////////////////////////////////////////////////////////////////////////////////
merge 1:1 LopNr_PersonNr year using "[FOLDER]\OccSort_Data.dta", keepusing(FodelseLandnamnMor FodelseLandnamnFar FodelseLandnamnMor FodelseLandnamnFar Y003_CF Y003_CM Y003 WVInd* nr_mp nr_ca rm rc )
drop if _merge==2
drop _merge
//Re-scale so that "labor types" don't turn negative. And to have cross-country stdv=1 for autonomy measure
replace Y003=(Y003+0.934001)/0.514
replace Y003_CF=(Y003_CF+0.934001)/0.514
replace Y003_CM=(Y003_CM+0.934001)/0.514


egen Y003_P = rowmean(Y003_CF Y003_CM ) 
replace Y003_P = 0 if  (FodelseLandnamnMor==107 | FodelseLandnamnMor==. ) & (FodelseLandnamnFar==107 | FodelseLandnamnFar==. )
///////////////////////////////////////////////////////////////////////////////////////////

replace Y003=0 if WVInd!=1

replace Y003_P=0 if WVInd_CF!=1 & WVInd_CM!=1

gen MigCat = 3 
replace MigCat = 1 if FodelseLandnamn!=107
replace MigCat = 2 if FodelseLandnamn==107 & ( (FodelseLandnamnFar!=107 & FodelseLandnamnFar!=.)   | (FodelseLandnamnMor!=107 & FodelseLandnamnMor!=.) )

gen WVSSwe=0
replace WVSSwe=1 if missing(WVInd_CF) & FodelseLandnamnMor==107
replace WVSSwe=1 if missing(WVInd_CM) & FodelseLandnamnFar==107
replace MigCat = 3 if MigCat==2 & WVSSwe==1


gen EU_Nat = EffUnits
replace EU_Nat =0 if MigCat!=3

gen EU_Sec = EffUnits
replace EU_Sec =0 if MigCat!=2

gen EU_Mig = EffUnits
replace EU_Mig =0 if MigCat!=1


gen EU_Y003 = EffUnits*Y003
replace EU_Y003 =0 if MigCat!=1

gen EU_Y003_P = EffUnits*Y003_P
replace EU_Y003_P =0 if MigCat!=2 | Kon!=0

gen nr = nr_mp+nr_ca -rm-rc
xtile nr_q=nr, nq(4)

gen NRGroup=1
replace NRGroup=2 if nr_q==3 | nr_q==4

keep LopNr_PeOrgNr year   KU1Ink  EffUnits EU_Nat EU_Sec  EU_Mig EU_Y003 EU_Y003_P tjomf_y_av ChildMig FodelseLandnamn Kon MigCat Y003 Y003_P u  NRGroup  



collapse (sum) KU1Ink  EffUnits EU_Nat EU_Sec EU_Mig EU_Y003 EU_Y003_P (count) countKU1=KU1Ink  (max) u=u,  by(LopNr_PeOrgNr year Kon NRGroup)


	sort LopNr_PeOrgNr year  
	by LopNr_PeOrgNr year: egen totcount = total(countKU1)
	
	forvalues i=1/2 {
		
	gen countM_NR`i' = 0
	gen countF_NR`i' = 0
	replace countM_NR`i'=countKU1 if Kon==0 & NRGroup==`i' 
	replace countF_NR`i'=countKU1 if Kon==1 & NRGroup==`i' 

	gen EU_Nat_M_NR`i' = 0 
	replace EU_Nat_M_NR`i' = EU_Nat if  Kon==0 & NRGroup==`i' 
	gen EU_Mig_M_NR`i' = 0 
	replace EU_Mig_M_NR`i' = EU_Mig if  Kon==0 & NRGroup==`i' 
	gen EU_Sec_M_NR`i' = 0 
	replace EU_Sec_M_NR`i' = EU_Sec if  Kon==0 & NRGroup==`i' 
	
	gen EU_Y003_NR`i' = 0 
	replace EU_Y003_NR`i' = EU_Y003 if  Kon==0 & NRGroup==`i' 
	
	
	gen EU_Y003_P_NR`i' = 0 
	replace EU_Y003_P_NR`i' = EU_Y003_P if  Kon==0 & NRGroup==`i' 

	gen EU_Nat_F_NR`i' = 0 
	replace EU_Nat_F_NR`i' = EU_Nat if  Kon==1 & NRGroup==`i' 
	gen EU_Mig_F_NR`i' = 0 
	replace EU_Mig_F_NR`i' = EU_Mig if  Kon==1 & NRGroup==`i' 
	gen EU_Sec_F_NR`i' = 0 
	replace EU_Sec_F_NR`i' = EU_Sec if  Kon==1 & NRGroup==`i' 
	
	}
	

drop countKU1	
collapse (max) count* totcount E*  , by(LopNr_PeOrgNr year)



merge 1:1 LopNr_PeOrgNr year using  "[FOLDER]\Panel_Firm.dta"

//Drop obs. w/o a firm				
drop if _merge!=3 
drop _merge



save "[FOLDER]\Panel_Firm_Lab_DirVar_v1.dta" , replace


drop if missing(Ftg_Foradlingsvarde)


		gen EU_Nat_2_A = (EU_Nat_M_NR2) *  3.601327  //number for Swe after "replace Y003=(Y003+0.934001)/0.514", i.e. setting to min 0 (to not have neg. labor) and stdv 1. 
		gen EU_Nat_1_A = (EU_Nat_M_NR1) *  3.601327  //number for Swe after "replace Y003=(Y003+0.934001)/0.514", i.e. setting to min 0 stdv 1. 

		gen lab_MH_1 = EU_Sec_M_NR2
		gen lab_MH_2 = EU_Mig_M_NR2
		gen lab_MH_3 = EU_Nat_M_NR2

		gen lab_ML_1 = EU_Sec_M_NR1
		gen lab_ML_2 = EU_Mig_M_NR1 
		
		
		
		gen lab_MH_C1 = EU_Y003_P_NR2 
		gen lab_MH_C2 = EU_Y003_NR2
		gen lab_MH_C3 =  EU_Nat_2_A  

		gen lab_ML_C1 = EU_Y003_P_NR1 
		gen lab_ML_C2 = EU_Y003_NR1
		gen lab_ML_C3 =  EU_Nat_1_A 



	egen M_Count=rowtotal(countM_*)
	egen F_Count=rowtotal(countF_*)
	gen totcount2=M_Count+F_Count
	
		gen RefGr =  EU_Nat_M_NR1 

		gen lab_FNat_H = EU_Nat_F_NR2 + EU_Sec_F_NR2
		gen lab_FFor_H = EU_Mig_F_NR2 
		gen lab_FNat_L = EU_Nat_F_NR1 + EU_Sec_F_NR1 
		gen lab_FFor_L = EU_Mig_F_NR1 


				
				
//Create needed vars:
drop if missing(Ftg_Foradlingsvarde)
gen logFtg_Foradlingsvarde=log(Ftg_Foradlingsvarde)
rename Ftg_Summa_bruttoinv GrosInvTot
gen logGrosInvTot = log(GrosInvTot)
gen logGrosInvTot_Sq = logGrosInvTot^2
gen logGrosInvTot_Qu = logGrosInvTot^3
  

gen Cap =  Ftg_Anlaggningstillgangar
gen logCap=log(Cap)
gen logCap_Sq=logCap^2
gen logCap_Qu=logCap^3

bysort Org_BelKommun: egen k_Size=sum(totcount)
sort k_Size
egen k_id =group(k_Size)
sum k_id
scalar maxid=r(max)
//City fixed effects; number 42 chosen to include FE for all counties with >50k inhabitants (the smallest urban area with a FE will have around 27k inhabitants). 
tab Org_BelKommun if k_id>maxid-42 ,gen(k_)
recode k_* (.=0)


rename totcount FirmSize

drop if FirmSize<6				
gen FirmSize_1=0
replace FirmSize_1=1 if FirmSize<16 & !missing(FirmSize) 
gen FirmSize_2=0
replace FirmSize_2=1 if FirmSize>15 & FirmSize<51 & !missing(FirmSize)
gen FirmSize_3=0
replace FirmSize_3=1 if FirmSize>50 & FirmSize<151 & !missing(FirmSize)
gen FirmSize_4=0
replace FirmSize_4=1 if FirmSize>150  & FirmSize<501 & !missing(FirmSize)
gen FirmSize_5=0
replace FirmSize_5=1 if FirmSize>500  & !missing(FirmSize)


tab year, gen(y_)
egen u = rowmiss(y_* FirmSize_* logFtg_Foradlingsvarde logCap logGrosInvTot )
gen Ind = substr(Org_Sni2007G,2,.)
destring Ind, replace
tab Ind, gen(Ind)



save "[FOLDER]\Panel_Firm_Lab_DirVarOcc_temp.dta" , replace


